package 代码随想录_回溯.数独;

/**
 * @author zx
 * @create 2022-08-12 13:12
 */
public class 有效的数独_36 {
    public boolean isValidSudoku(char[][] board) {
        for(int i = 0;i < 9;i++){
            for(int j = 0;j < 9;j++){
                if(board[i][j] >= '0' && board[i][j] <= '9'){
                    if(!isValid(board,i,j)){
                        return false;
                    }
                }
            }
        }
        return true;
    }
    private boolean isValid(char[][] board, int row, int col){
        //判断行
        for(int i = 0;i < 9;i++){
            if(board[i][col] == board[row][col] && i != row){
                return false;
            }
        }
        //判断列
        for(int i = 0;i < 9;i++){
            if(board[row][i] == board[row][col] && i != col){
                return false;
            }
        }
        //判断九宫格
        int heng = (row / 3) * 3;
        int zong = (col / 3) * 3;
        for(int i = heng;i < heng + 3;i++){
            for(int j = zong;j < zong + 3;j++){
                if(board[i][j] == board[row][col] && i != row && j != col){
                    return false;
                }
            }
        }
        return true;
    }
}
